import axios from 'axios'
import { Toast } from 'vant'
import router from '../router'

// axios.defaults.baseURL = process.env.NODE_ENV === 'production' ? process.env.VUE_APP_BASE_API + '/api/v2' : '/api/v2', // api 的 base_url
// axios.defaults.baseURL = '/api/v2'
// axios.defaults.baseURL = 'http://test6.megowx.com/api/v2'
axios.defaults.baseURL = 'http://rhc.test/api/v2'
//Access-Control-Allow-Origin 
// axios.defaults.withCredentials = true   这是关键
axios.defaults.headers['X-Requested-With'] = 'XMLHttpRequest'
axios.defaults.headers['token'] = localStorage.getItem('token') || ''
axios.defaults.headers.post['Content-Type'] = 'application/json'

axios.interceptors.response.use(res => {
  // console.log(res);
  if (typeof res.data !== 'object') {
    Toast.fail('服务端异常！')
    return Promise.reject(res)
  }
  if (res.status != 200) {
    if (res.ErrMsg) Toast.fail(res.ErrMsg)
	// if (res.data.code == 416) {
 //      router.push({ path: '/login' })
 //    }
    return Promise.reject(res.data)
  }

  return res.data
})

export default axios
